package com.jerry.huihome.module.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseViewHolder;
import com.jerry.huihome.base.MyBaseAdapter;
import com.jerry.huihome.module.find_near_mine.activity.AfflatuspictoActivity;
import com.jerry.huihome.module.home.entity.DrawHouse;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.BindView;

/**
 * Created by LGuipeng on 2016/5/10.
 */
public class HomeBottomAdapter extends MyBaseAdapter{

    private final Context context;
    private final DisplayImageOptions options;
    private final ImageLoader imageLoader;

    public HomeBottomAdapter(Context context, List list){
        super(context, list);
        this.context = context;
        options = BaseApplication.getApplication().getImageOptions();
        imageLoader = BaseApplication.getApplication().getImageLoader();
    }

    @Override
    public int getLayoutId(){
        return R.layout.home_item_layout;
    }

    @Override
    public BaseViewHolder getViewHolder(){
        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder){
        DrawHouse.BodyBean.PicturesBean picturesBean = null;
        DrawHouse.BodyBean.ProjectsBean projectsBean = null;
        final Object item = getItem(viewHolder.position);
        ViewHolder holder = (ViewHolder) viewHolder;
        if(item instanceof DrawHouse.BodyBean.PicturesBean){
            picturesBean = (DrawHouse.BodyBean.PicturesBean) item;
            imageLoader.displayImage(picturesBean.getCoverimg(),holder.imageView,options);
            final int id = picturesBean.getId();
            holder.imageView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    if(item instanceof DrawHouse.BodyBean.PicturesBean){
                        Intent intent = new Intent(context, AfflatuspictoActivity.class);
                        intent.putExtra("id", id);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        ((Activity)context).startActivity(intent);
                    }
                }
            });
            holder.tvName.setText(picturesBean.getWorkname());
            holder.itemCollected.setText(""+ picturesBean.getAppraiseCount());
            holder.itemReply.setText(picturesBean.getCommentCount()+"");
            holder.itemShare.setText(picturesBean.getShareCount()+"");
        }else if(item instanceof DrawHouse.BodyBean.ProjectsBean){
            projectsBean = (DrawHouse.BodyBean.ProjectsBean) item;
            imageLoader.displayImage(projectsBean.getPhaseImg(),holder.imageView,options);
            final int projid = projectsBean.getProjid();
            holder.imageView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    if(item instanceof DrawHouse.BodyBean.PicturesBean){
                        /*Intent intent = new Intent(context, ForemanDetailActivity.class);
                        intent.putExtra("pmid", projid +"");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        ((Activity)context).startActivity(intent);*/
                    }
                }
            });
            holder.tvName.setText(projectsBean.getProjname());
            holder.itemCollected.setText(""+ projectsBean.getAppraiseCount());
            holder.itemReply.setText(projectsBean.getCommentCount()+"");
            holder.itemShare.setText(projectsBean.getShareCount()+"");
        }
    }
    public static class ViewHolder extends BaseViewHolder
    {
        @BindView(R.id.iv_home_item)
        ImageView imageView;

        @BindView(R.id.tv_home_item_name)
        TextView tvName;

        @BindView(R.id.rb_home_item_collected)
        RadioButton itemCollected;

        @BindView(R.id.rb_home_item_reply)
        RadioButton itemReply;

        @BindView(R.id.rb_home_item_share)
        RadioButton itemShare;
    }
}
